/****private function***/

function __init(){
	//$addEvent($('userList') , "click" , setActionWindow);
}

function modFn(el){
	var text = http_request.responseText;
	if(text.indexOf('fail')!=-1){
		alert("Update Failed!");
		var tr = el.parentNode.parentNode;
		var inps = tr.getElementsByTagName("input");
		inps[0].className = inps[1].className = inps[2].className = "showInput";
		inps[0].readOnly = inps[1].readOnly = inps[2].readOnly = false;
		inps[3].style.display = "none";
		inps[4].style.display = "";
	}
	else{
		alert("Update Succeed!");
	}
}

function delFn(){
	var text = http_request.responseText;
	var trs = $('userList').getElementsByTagName('tr');
	for(var i=1 , l=trs.length-2;i<l;i++){
		if(trs[i].getAttribute("tag")==text)	
			$('userList').getElementsByTagName('tbody')[0].removeChild(trs[i]);
	}
	alert("Delete Succeed!");
}

function setActionWindow(e){
	var el = getSrcEl(e);
	if(el.tagName=="INPUT"){
		var action=el.getAttribute("tag");
		var userid = el.getAttribute("userid");
		if(action=="add"){
			showAddWindow();
		}
		if(action=="modify"){
			makeModifyUser(el);
		}
		if(action=="saveModify"){
			ajaxModifyUser(el , userid);
		}
		if(action=="delete"){
			ajaxDeleteUser(userid);
		}
	}
}


function makeModifyUser(el){
	var tr = el.parentNode.parentNode;
	var inps = tr.getElementsByTagName("input");
	inps[0].className = inps[1].className = inps[2].className = "showInput";
	inps[0].readOnly = inps[1].readOnly = inps[2].readOnly = false;
	inps[3].style.display = "none";
	inps[4].style.display = "";
}

function ajaxModifyUser(el , uid){
	var tr = el.parentNode.parentNode;
	var tds = tr.getElementsByTagName("td");
	var inps = tr.getElementsByTagName("input");
	var defw = inps[0].value.trim();
	var defh = inps[1].value.trim();
	var defq = inps[2].value.trim();
	
	if(defw==""||defh==""||defq==""){
		alert("please fulfill the require fields");
		return;
	}

	inps[0].className = inps[1].className = inps[2].className = "hideInput";
	inps[0].readOnly = inps[1].readOnly = inps[2].readOnly = true;
	inps[3].style.display = "";
	inps[4].style.display = "none";

	var action = "action=moddpfuser";
	var defw = "&defw="+encodeURI(defw);
	var defh = "&defh="+encodeURI(defh);
	var defq = "&defq="+encodeURI(defq);
	var param = action+defw+defh+defq;
	makeAjaxRequest(ajaxUrl , param , true , modFn.pass(el));
}

function ajaxDeleteUser(uid){
	var action = "action=deldpfuser";
	var userid = "&userid="+encodeURI(uid);
	var param = action+userid;
	makeAjaxRequest(ajaxUrl , param , true , delFn);
}

__domready();